﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Win.Forms
{
    public partial class RestReportForm : ReportBaseForm
    {
        public RestReportForm()
        {
            InitializeComponent();
            InitializeViewUI();
            InitializeBinding();
        }

        protected virtual DataTable GetRestData()
        {
            var sql = @"SELECT FACCCODE,CONVERT(nvarchar(2), FRANGE+1) FRANGE,sum(FDEBITAMOUNTBQ) SUMJ,sum(FCREDITAMOUNTBQ) SUMD,sum(FQUANTITYDEBITBQ) SUMQJ,sum(FQUANTITYCREDITBQ) SUMQD
                        FROM TREST
                        group by FRANGE,FACCCODE";
            return Agile.Engine.Data.DataContext.Get("CacheDataContext").DefaultModel.QueryTable(CommandType.Text, "RestData", sql);

        }

        protected RestReportForm(int child)
        {
            InitializeComponent();
        }

        protected override void ShowFilter()
        {
            var f = new ReportFilterForm();

            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ///获取周期过滤条件
                RangeFilter = f.RangeFilter;
                ///获取科目过滤条件
                AccFilter = (f.AccountFilter == null) ? string.Empty : f.AccountFilter.ToString();

                InitializeBinding();
            }



        }

        public override void InitializeBinding()
        {
            var xml = Agile.Engine.ObjectContext.GetResource("SqlResource");
            var filter = string.IsNullOrEmpty(AccFilter) ? string.Empty : string.Format(" And a.FACCCODE = '{0}'", AccFilter);
            var sql = xml.Format("report_rest", RangeFilter, filter);

            var data = Agile.Engine.Data.DataContext.Get("CacheDataContext").DefaultModel.QueryTable(CommandType.Text, "RestReport", sql);
            data.PrimaryKey = new DataColumn[] { data.Columns["FRANGE"], data.Columns["FACCCODE"] };
            var rest = GetRestData();
            rest.PrimaryKey = new DataColumn[] { rest.Columns["FRANGE"], rest.Columns["FACCCODE"] };
            data.Merge(rest, false, MissingSchemaAction.Add);
            data.DefaultView.RowFilter = "NOT( FDEBITAMOUNTBQ = FCREDITAMOUNTBQ AND (FCREDITAMOUNTBQ = 0) )";


            gridControl1.DataSource = data;
        }

        public override void InitializeViewUI()
        {
            theGridControl = gridControl1;

            RangeFilter = (int.Parse(Fis.Win.Utils.AppSession.Range) - 1).ToString();
        }

        protected override void InitializeResetColumns(DataTable data)
        {

        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;

            var data = gridView1.GetFocusedDataRow();
            ShowDetial(data);
        }

        protected override void ShowView()
        {
            gridControl1_DoubleClick(null, null);
        }

        protected override void ShowDetial(object state)
        {
            var accID = (state as DataRow)[1].ToString();
            var f = new Fis.Win.Forms.DetailReportForm();
            f.RangeFilter = RangeFilter;
            f.ResetAccountInfo(accID);
            f.InitializeBinding();
            f.ShowDialog();
        }
    }
}
